package com.bao.shop.service;

import com.bao.shop.entity.UserInfo;
import com.bao.shop.service.impl.UserInfoManager;

import java.util.Random;

public class UserInfoManagerImpl implements UserInfoManager {
    @Override
    public void inital(){
        USERS.add(new UserInfo("1001","马禾禾","铁金卡",2000,"合法账户","123"));
        USERS.add(new UserInfo("1003","弃世猴","钻石卡",5000,"被禁账户","123"));
        USERS.add(new UserInfo("1002","弓工工","白金卡",2000,"合法账户","123"));
    }
    @Override
    public void mainMenu(){
        System.out.println("====================欢迎使用超市用户管理系统====================");
        System.out.println("1.查询会员信息");
        System.out.println("2.删除会员");
        System.out.println("3.新增用户信息");
        System.out.println("4.退出");
        System.out.println("=============================================================");
    }
    @Override
    public void searchAll(){
        System.out.println("用户ID\t 账号\t 卡类型\t 积分\t 用户状态\t");
        for (UserInfo user : USERS) {
            System.out.println(user.toString());
        }
    }

    @Override
    public void deleteUserInfo() {
        System.out.println("请输入需要删除的用户ID");
        UserInfo newUser=new UserInfo(INPUT.next());
        if(!USERS.contains(newUser)){
            System.err.println("用户不存在");
            return;
        }

        getUserInfo(newUser,"删除");
        if(continueSystem()){
            System.err.println("退出操作");
            return;
        }
        if(USERS.remove(newUser)){
            System.out.println("删除成功");
        }else {
            System.err.println("删除失败");
        }
    }

    @Override
    public void addUserInfo() {
        String trueUid;
        while(true){
            String newUid= String.valueOf(new Random().nextInt(9000)+1000);
            UserInfo newUser=new UserInfo(newUid);
            if(!USERS.contains(newUser)){
                trueUid=newUid;
                break;
            }
        }
        System.out.println("请输入用户名");
        String name=INPUT.next();
        System.out.println("请输入卡类型");
        String cardType=INPUT.next();
        System.out.println("请输入积分");
        int integral=INPUT.nextInt();
        System.out.println("请输入用户状态");
        String userStatus=INPUT.next();
        System.out.println("请输入密码");
        String password=INPUT.next();
        System.out.println("即将添加的用户信息为");
        System.out.println("用户ID\t 账号\t 卡类型\t 积分\t 用户状态\t");
        UserInfo newUserInfo=new UserInfo(trueUid,name,cardType,integral,userStatus,password);
        System.out.println(newUserInfo);
        if(continueSystem()){
            return;
        }
        if(USERS.add(newUserInfo)){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    private boolean continueSystem(){
        System.out.println("是否确认操作?(y/n)");
        return !"y".equals(INPUT.next());
    }

    private void getUserInfo(UserInfo newUser,String str){
        for (UserInfo user : USERS) {
            if(user.equals(newUser)){
                newUser=user;
            }
        }
        System.out.println("你要"+str+"的信息如下");
        System.out.println(newUser.toString());
    }


}
